﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Bangle.DBUtility;
namespace Bangle.DAL
{
    //T_Phone
    public partial class T_PhoneDAL
    {

        public bool Exists(Guid PhoneId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Phone");
            strSql.Append(" where ");
            strSql.Append(" PhoneId = @PhoneId  ");
            SqlParameter[] parameters = {
					new SqlParameter("@PhoneId", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = PhoneId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Bangle.Model.T_Phone model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Phone(");
            strSql.Append("PhoneId,PhoneName,PhoneCode,SupplierId,RMB,PartNo,Specification,USD,PCS,CreateUserId,CreateTime,UpdateUserId,UpdateTime,Valid,ScreenId,ChipId,SeriesId,SalePoint");
            strSql.Append(") values (");
            strSql.Append("@PhoneId,@PhoneName,@PhoneCode,@SupplierId,@RMB,@PartNo,@Specification,@USD,@PCS,@CreateUserId,@CreateTime,@UpdateUserId,@UpdateTime,@Valid,@ScreenId,@ChipId,@SeriesId,@SalePoint");
            strSql.Append(") ");

            SqlParameter[] parameters = {
			            new SqlParameter("@PhoneId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@PhoneName", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@PhoneCode", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@SupplierId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@RMB", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PartNo", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Specification", SqlDbType.VarChar) ,            
                        new SqlParameter("@USD", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PCS", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4) ,            
                        new SqlParameter("@ScreenId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ChipId", SqlDbType.Int,4) ,            
                        new SqlParameter("@SeriesId", SqlDbType.Int,4) ,            
                        new SqlParameter("@SalePoint", SqlDbType.VarChar,8000)             
              
            };

            parameters[0].Value = model.PhoneId;
            parameters[1].Value = model.PhoneName;
            parameters[2].Value = model.PhoneCode;
            parameters[3].Value = model.SupplierId;
            parameters[4].Value = model.RMB;
            parameters[5].Value = model.PartNo;
            parameters[6].Value = model.Specification;
            parameters[7].Value = model.USD;
            parameters[8].Value = model.PCS;
            parameters[9].Value = model.CreateUserId;
            parameters[10].Value = model.CreateTime;
            parameters[11].Value = model.UpdateUserId;
            parameters[12].Value = model.UpdateTime;
            parameters[13].Value = model.Valid;
            parameters[14].Value = model.ScreenId;
            parameters[15].Value = model.ChipId;
            parameters[16].Value = model.SeriesId;
            parameters[17].Value = model.SalePoint;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Bangle.Model.T_Phone model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Phone set ");

            strSql.Append(" PhoneId = @PhoneId , ");
            strSql.Append(" PhoneName = @PhoneName , ");
            strSql.Append(" PhoneCode = @PhoneCode , ");
            strSql.Append(" SupplierId = @SupplierId , ");
            strSql.Append(" RMB = @RMB , ");
            strSql.Append(" PartNo = @PartNo , ");
            strSql.Append(" Specification = @Specification , ");
            strSql.Append(" USD = @USD , ");
            strSql.Append(" PCS = @PCS , ");
            strSql.Append(" CreateUserId = @CreateUserId , ");
            strSql.Append(" CreateTime = @CreateTime , ");
            strSql.Append(" UpdateUserId = @UpdateUserId , ");
            strSql.Append(" UpdateTime = @UpdateTime , ");
            strSql.Append(" Valid = @Valid , ");
            strSql.Append(" ScreenId = @ScreenId , ");
            strSql.Append(" ChipId = @ChipId , ");
            strSql.Append(" SeriesId = @SeriesId , ");
            strSql.Append(" SalePoint = @SalePoint  ");
            strSql.Append(" where PhoneId=@PhoneId  ");

            SqlParameter[] parameters = {
			            new SqlParameter("@PhoneId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@PhoneName", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@PhoneCode", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@SupplierId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@RMB", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PartNo", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Specification", SqlDbType.VarChar) ,            
                        new SqlParameter("@USD", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PCS", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4) ,            
                        new SqlParameter("@ScreenId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ChipId", SqlDbType.Int,4) ,            
                        new SqlParameter("@SeriesId", SqlDbType.Int,4) ,            
                        new SqlParameter("@SalePoint", SqlDbType.VarChar,8000)             
              
            };

            parameters[0].Value = model.PhoneId;
            parameters[1].Value = model.PhoneName;
            parameters[2].Value = model.PhoneCode;
            parameters[3].Value = model.SupplierId;
            parameters[4].Value = model.RMB;
            parameters[5].Value = model.PartNo;
            parameters[6].Value = model.Specification;
            parameters[7].Value = model.USD;
            parameters[8].Value = model.PCS;
            parameters[9].Value = model.CreateUserId;
            parameters[10].Value = model.CreateTime;
            parameters[11].Value = model.UpdateUserId;
            parameters[12].Value = model.UpdateTime;
            parameters[13].Value = model.Valid;
            parameters[14].Value = model.ScreenId;
            parameters[15].Value = model.ChipId;
            parameters[16].Value = model.SeriesId;
            parameters[17].Value = model.SalePoint;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid PhoneId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Phone ");
            strSql.Append(" where PhoneId=@PhoneId ");
            SqlParameter[] parameters = {
					new SqlParameter("@PhoneId", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = PhoneId;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Bangle.Model.T_Phone GetModel(Guid PhoneId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PhoneId, PhoneName, PhoneCode, SupplierId, RMB, PartNo, Specification, USD, PCS, CreateUserId, CreateTime, UpdateUserId, UpdateTime, Valid, ScreenId, ChipId, SeriesId, SalePoint  ");
            strSql.Append("  from T_Phone ");
            strSql.Append(" where PhoneId=@PhoneId ");
            SqlParameter[] parameters = {
					new SqlParameter("@PhoneId", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = PhoneId;


            Bangle.Model.T_Phone model = new Bangle.Model.T_Phone();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["PhoneId"].ToString() != "")
                {
                    model.PhoneId = new Guid(ds.Tables[0].Rows[0]["PhoneId"].ToString());
                }
                model.PhoneName = ds.Tables[0].Rows[0]["PhoneName"].ToString();
                model.PhoneCode = ds.Tables[0].Rows[0]["PhoneCode"].ToString();
                if (ds.Tables[0].Rows[0]["SupplierId"].ToString() != "")
                {
                    model.SupplierId = new Guid(ds.Tables[0].Rows[0]["SupplierId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RMB"].ToString() != "")
                {
                    model.RMB = decimal.Parse(ds.Tables[0].Rows[0]["RMB"].ToString());
                }
                model.PartNo = ds.Tables[0].Rows[0]["PartNo"].ToString();
                model.Specification = ds.Tables[0].Rows[0]["Specification"].ToString();
                if (ds.Tables[0].Rows[0]["USD"].ToString() != "")
                {
                    model.USD = decimal.Parse(ds.Tables[0].Rows[0]["USD"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PCS"].ToString() != "")
                {
                    model.PCS = decimal.Parse(ds.Tables[0].Rows[0]["PCS"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreateUserId"].ToString() != "")
                {
                    model.CreateUserId = int.Parse(ds.Tables[0].Rows[0]["CreateUserId"].ToString());
                }
                model.CreateTime = ds.Tables[0].Rows[0]["CreateTime"].ToString();
                if (ds.Tables[0].Rows[0]["UpdateUserId"].ToString() != "")
                {
                    model.UpdateUserId = int.Parse(ds.Tables[0].Rows[0]["UpdateUserId"].ToString());
                }
                model.UpdateTime = ds.Tables[0].Rows[0]["UpdateTime"].ToString();
                if (ds.Tables[0].Rows[0]["Valid"].ToString() != "")
                {
                    model.Valid = int.Parse(ds.Tables[0].Rows[0]["Valid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ScreenId"].ToString() != "")
                {
                    model.ScreenId = int.Parse(ds.Tables[0].Rows[0]["ScreenId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ChipId"].ToString() != "")
                {
                    model.ChipId = int.Parse(ds.Tables[0].Rows[0]["ChipId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SeriesId"].ToString() != "")
                {
                    model.SeriesId = int.Parse(ds.Tables[0].Rows[0]["SeriesId"].ToString());
                }
                model.SalePoint = ds.Tables[0].Rows[0]["SalePoint"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Phone ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM T_Phone ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_Phone ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.MenuId desc");
            }
            strSql.Append(")AS Row, T.*  from T_Phone T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

